package logic

import (
	"ball/ballrpc/ballrpc"
	"ball/ballrpc/internal/svc"
	"ball/models/mysql"
	"context"
	"fmt"

	"github.com/zeromicro/go-zero/core/logx"
)

type PasswordLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewPasswordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PasswordLogic {
	return &PasswordLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 找回密码
func (l *PasswordLogic) Password(in *ballrpc.PasswordRequest) (*ballrpc.PasswordResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User
	mysql.DB.Debug().Where("account=?", in.Account).Find(&user)
	if user.ID == 0 {
		return nil, fmt.Errorf("该用户不存在")
	}
	if in.Password != in.NewPassword {
		return nil, fmt.Errorf("确认密码与第一次不一致")
	}
	if in.NewPassword == user.Password {
		return nil, fmt.Errorf("新密码不能与旧密码一致")
	}
	user.Password = in.NewPassword
	mysql.DB.Debug().Updates(&user)
	return &ballrpc.PasswordResponse{
		Id: int64(user.ID),
	}, nil
}
